////
////  RBAppDelegateHelper.m
////  基本结构
////
////  Created by 蓝其 on 16/10/19.
////  Copyright © 2016年 蓝其. All rights reserved.
////
//
#import "RBAppDelegateHelper.h"

#import "RBTabBarController.h"
#import "RBCommerceTabBarVC.h"
#import "RBTabBarButton.h"

#import "RBGuideView.h"
#import "RBDefine.h"
#import "RBAccountManager.h"
#import "RBPersistence.h"
#import "RBThemeConfig.h"
#import <YYCategories/UIImage+YYAdd.h>
#import <IQKeyboardManager.h>
#import "NSString+RBAdd.h"
#import "UIView+RBAdd.h"
#import <UIView+YYAdd.h>
#import "UINavigationController+RBAdd.h"
#import <UIColor+YYAdd.h>
#import <NSObject+YYAdd.h>

#import "RBHomeVC.h"
#import "RBCategoryVC.h"
#import "RBShopVC.h"
#import "RBCartVC.h"
#import "RBMineVC.h"
#import "RBLoginVC.h"


@implementation RBAppDelegateHelper

+ (void)load
{
    __block id observer = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidFinishLaunchingNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note){
        [self setup];
        [[NSNotificationCenter defaultCenter] removeObserver:observer];
    }];
}

+ (void)setup
{
    [self setNavigationBarAppearance];
    [self setTabBarAppearance];
    [self setRootViewController];
}

+ (Class)vcClassWithName: (NSString *)className
{
    Class vcClass = NSClassFromString(className);
    return vcClass? vcClass: [UIViewController class];
}

#pragma mark - Appearance

+ (void)setNavigationBarAppearance
{
    UIColor *itemColor = [UIColor colorWithHexString:@"#222222"];
    UINavigationBar *navigationBar = [UINavigationBar appearance];
    //title颜色
    navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: itemColor, NSFontAttributeName: [RBThemeConfig navigationTitleFont]};
    
    //bar颜色和bar下面阴影颜色
    navigationBar.translucent = NO;
    [navigationBar setBackgroundImage:[UIImage imageWithColor:UIColor.whiteColor] forBarMetrics:UIBarMetricsDefault];
    navigationBar.shadowImage = [UIImage imageWithColor:UIColor.whiteColor];
    
    //item文字
    UIBarButtonItem *item = [UIBarButtonItem appearance];
    [item setTitleTextAttributes:@{NSForegroundColorAttributeName: itemColor, NSFontAttributeName: [RBThemeConfig navigationItemFont]} forState:UIControlStateNormal];
    [item setTitleTextAttributes:@{NSForegroundColorAttributeName: itemColor, NSFontAttributeName: [RBThemeConfig navigationItemFont]} forState:UIControlStateHighlighted];
    //item返回箭头颜色
    navigationBar.tintColor = itemColor;
    
    
    if(@available(iOS 13.0,*))
    {
        UINavigationBarAppearance *ap = UINavigationBarAppearance.new;
        ap.titleTextAttributes = navigationBar.titleTextAttributes;
        ap.backgroundImage = [UIImage imageWithColor:UIColor.whiteColor];
        ap.shadowImage = [UIImage imageWithColor:UIColor.whiteColor];
        
        navigationBar.standardAppearance = ap;
        navigationBar.scrollEdgeAppearance = ap;
    }
    
    //item返回图标 ....
//    navigationBar.backIndicatorImage = [UIImage imageNamed:@"backArror"];
//    navigationBar.backIndicatorTransitionMaskImage = [UIImage imageNamed:@"backArror"];
    /*
     (1)导航栏透明问题:
     1.translucent = NO,强制不透明,barTintColor就不会变色了
     2.- (void)setBackgroundImage:(nullable UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics
     这个取决于backgroundImage透明度,如果这个图片有透明度导航栏就透明,否则不透明
     
     (2)导航栏阴影问题:
     - (void)setBackgroundImage:(nullable UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics
     设置以后shadowImage才生效
     
     (3)控制器中view的起点问题:
     如果导航栏透明,起点从屏幕最上方开始,否则从导航栏下方开始,是否透明可以通过translucent
     判断,如果setBackgroundImage设置了不透明图片,translucent也会被设置为不透明
     
     (4)推论,tabBar问题同理
     */
}

+ (void)setTabBarAppearance
{
    UITabBar *tabBar = [UITabBar appearance];
    tabBar.translucent = NO;
    
//给默认颜色
//    tabBar.backgroundImage = [UIImage imageWithColor:[UIColor clearColor]];
//    tabBar.backgroundImage = [UIImage imageNamed:@"一个半透明,图片"];
    
//    UIImage *image = [[UIView blurView] snapshotImage];
//    tabBar.backgroundImage = image;
//    tabBar.shadowImage = image;
    
//因为要自定义,所以这块不设置
//    UITabBarItem *item = [UITabBarItem appearance];
//    [item setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateNormal];
//    [item setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateSelected];
}

#pragma mark - RootController

+ (void)setRootViewController
{
    UIWindow *keyWindow = [self keyWindow];
    
    
    
    if(0)
    {
        UIViewController *vc = NSClassFromString(@"RBShopVC").new;
        UINavigationController *naviVc = [UINavigationController.alloc initWithRootViewController:vc];
        keyWindow.rootViewController = naviVc;
    }
    else
    {
        keyWindow.rootViewController = [self tabbarVC];
//        keyWindow.rootViewController = [self loginVC];
    }
        
//    keyWindow.rootViewController = [UINavigationController.alloc initWithRootViewController:[NSClassFromString(@"RBVC1") new]];
    
    return;
    
    //决定要不要加一个引导页
    NSString *bundleVersion = [RBPersistence getProjectVersion];
    NSString *currentVersion = [RBPersistence getStoredVersion];
    if([bundleVersion biggerThanVersion:currentVersion])
    {//1.新版本,加新特性界面遮罩
        keyWindow.rootViewController = [self loginVC];
        RBGuideView *guideView = [[RBGuideView alloc] initWithFrame:kRBMainScreenBounds];
        [keyWindow addSubview:guideView];
    }
    else
    {//2.无更新
        if (0)
        {//1.有用户名密码,载入页发请求登录
//            keyWindow.rootViewController = [RBLoadingView new];
        }
        else
        {//2.无用户名密码,进入登录页
            keyWindow.rootViewController = [self loginVC];
        }
    }
}

+ (UIViewController *)loginVC
{
//    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    
    //这里设置loginVC
    UIViewController *vc = [[self vcClassWithName:@"RBLoginVC"] new];
    UINavigationController *naviVC = [[UINavigationController alloc] initWithRootViewController:vc];
    
//    UINavigationBar *navigationBar = naviVC.navigationBar;
    //title颜色
//    navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [RBThemeConfig textBlackColor], NSFontAttributeName: [RBThemeConfig navigationTitleFont]};
    //bar颜色和bar下面阴影颜色
//    [navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor]] forBarMetrics:UIBarMetricsDefault];
//    navigationBar.shadowImage = [UIImage imageWithColor:[UIColor clearColor]];
    //item返回箭头颜色
//    navigationBar.tintColor = [RBThemeConfig textBlackColor];
    //item文字
//    UIBarButtonItem *item = [UIBarButtonItem appearance];
//    [item setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor], NSFontAttributeName: [RBThemeConfig navigationItemFont]} forState:UIControlStateNormal];
//    [item setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor], NSFontAttributeName: [RBThemeConfig navigationItemFont]} forState:UIControlStateHighlighted];
    return naviVC;
}

+ (UIViewController *)tabbarVC
{
//    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    
    RBTabBarController *tabBarController = [RBTabBarController new];
    [tabBarController setViewControllers:
     @[[self controllerWithClass:RBHomeVC.class title:@"首页"],
       [self controllerWithClass:RBCategoryVC.class title:@"分类"],
       [self controllerWithClass:RBShopVC.class title:@"店铺"],
       [self controllerWithClass:RBCartVC.class title:@"购物车"],
       [self controllerWithClass:RBMineVC.class title:@"我的"],
       ]];
    [tabBarController setTabBarButtons:@[
        [self buttonWithTitle:@"首页" normalImage:[UIImage imageNamed:@"n1.png"] selectedImage:[UIImage imageNamed:@"n1s.png"] buttonClass:RBTabBarButton.class special:NO],
        [self buttonWithTitle:@"分类" normalImage:[UIImage imageNamed:@"n2.png"] selectedImage:[UIImage imageNamed:@"n2s.png"] buttonClass:RBTabBarButton.class special:NO],
        [self buttonWithTitle:@"店铺" normalImage:[UIImage imageNamed:@"n3.png"] selectedImage:[UIImage imageNamed:@"n3s.png"] buttonClass:RBTabBarButton.class special:NO],
        [self buttonWithTitle:@"购物车" normalImage:[UIImage imageNamed:@"n4.png"] selectedImage:[UIImage imageNamed:@"n4s.png"] buttonClass:RBTabBarButton.class special:NO],
        [self buttonWithTitle:@"我的" normalImage:[UIImage imageNamed:@"n5.png"] selectedImage:[UIImage imageNamed:@"n5s.png"] buttonClass:RBTabBarButton.class special:NO],
    ] withIndexesOfViewController:@[@0,@1,@2,@3,@4] proportionsOfTabBarWidth:@[@0.2,@0.2,@0.2,@0.2,@0.2]];
    tabBarController.autoSelectVC = NO;
    tabBarController.tabBarButtonTappedBlock = ^(RBTabBarController * _Nullable controller, UIButton * _Nullable button, NSInteger currentIndex, NSInteger fromIndex) {
        if(currentIndex == 3 || currentIndex == 4)
        {
            if([[RBAccountManager getUserId] length] == 0){
                [self showLoginPageWithCompletion:^{

                }];
            } else {
                controller.selectedTabBarButtonIndex = currentIndex;
            }
        }
        else
        {
            controller.selectedTabBarButtonIndex = currentIndex;
        }
    };
    return tabBarController;
}

+ (UIWindow *)keyWindow
{
    UIWindow *window = [[UIApplication sharedApplication].delegate window];
    NSAssert(window , @"Something is wrong, check it out");
    return window;
}

+ (UINavigationController *)controllerWithClass: (Class)controllerClass title: (NSString *)title
{
    UIViewController *controller = [controllerClass new];
    controller.title = title;
    return [[UINavigationController alloc] initWithRootViewController:controller];
}

+ (UIButton *)buttonWithTitle: (NSString *)title normalImage: (UIImage *)normalImage selectedImage: (UIImage *)selectedImage buttonClass: (Class)buttonClass special: (BOOL)isSpecial
{
    UIColor *normalColor = [RBThemeConfig tabBarNormalColor];
    UIColor *selectedColor = [RBThemeConfig tabBarSelectColor];
    RBTabBarButton *button = (RBTabBarButton *)[buttonClass new];
    button.special = isSpecial;
    button.titleLabel.font = [RBThemeConfig tabBarTextFont];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:normalColor forState:UIControlStateNormal];
    [button setTitleColor:selectedColor forState:UIControlStateSelected];
    [button setTitleColor:selectedColor forState:UIControlStateHighlighted | UIControlStateSelected];
    [button setImage:normalImage forState:UIControlStateNormal];
    [button setImage:selectedImage forState:UIControlStateSelected];
    [button setImage:selectedImage forState:UIControlStateHighlighted | UIControlStateSelected];
    return button;
}

+ (void)showLoginPageWithCompletion:(void (^)(void))completion
{
    RBLoginVC *vc = RBLoginVC.new;
    UINavigationController *naviVc = [UINavigationController.alloc initWithRootViewController:vc];
    [[self keyWindow].rootViewController presentViewController:naviVc animated:YES completion:nil];
}

@end
